package com.origin.onlinesys.web.controllers.modules.account;

import com.origin.onlinesys.common.controllers.BaseController;
import com.origin.onlinesys.domain.modules.interviewsys.MenuBean;
import com.origin.onlinesys.domain.modules.interviewsys.Permission;
import com.origin.onlinesys.service.modules.user.impl.MenuServiceImpl;
import com.origin.onlinesys.service.modules.user.impl.PermissionServiceImplShiro;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/2/23.
 * 菜单
 */
@Controller
@RequestMapping(value = "management/per")
public class PermissionController extends BaseController {

    @Autowired
    private PermissionServiceImplShiro service;

    @Autowired
    private MenuServiceImpl menuService;

    @RequestMapping(value = "/add")
    public @ResponseBody
    int addPer(@ModelAttribute Permission permission,@RequestParam("menuId[]")ArrayList<String> menuId){
        permission.setState("1");service.save(permission);
        List<Map> list = new ArrayList();Map map = null;
        for(String id:menuId){
            map = new HashMap();map.put("pId",permission.getId());map.put("mId",id);
            list.add(map);
        }
        service.addMenuPermission(list);this.delNavRole();
        return 1;
    }

    @RequestMapping(value = "/findItem")
    public @ResponseBody
    List<Permission> findItem(){
        return service.selectItem();
    }

    @RequestMapping(value = "/findInfo")
    public @ResponseBody
    Map findInfo(String id){
        List<MenuBean> menuBeans = menuService.selectAll();
        List<String> mId =  service.selectMIdByPId(id);
        for(int j=0;j<menuBeans.size();j++){
            for(int i=0;i<mId.size();i++){
                if(mId.get(i).equals(menuBeans.get(j).getId())){
                    menuBeans.get(j).setCheck(true);break;
                }
            }
        }
        Map map = new HashMap();
        map.put("info", service.selectById(id));
        map.put("menu",menuBeans);
        return map;
    }

    @RequestMapping(value = "/update")
    public @ResponseBody
    int update(@ModelAttribute Permission permission,@RequestParam("menuId[]")ArrayList<String> menuId){
        service.update(permission);
        service.delMenuPermission(permission.getId());
        List<Map> list = new ArrayList();Map map = null;
        for(String id:menuId){
            map = new HashMap();map.put("pId",permission.getId());map.put("mId",id);
            list.add(map);
        }
        service.addMenuPermission(list);this.delNavRole();
        return 1;
    }

    @RequestMapping(value = "/findAll")
    public @ResponseBody
    List<Permission> findAll(){
        Permission permission = new Permission();
        permission.setState("1");
        return service.selectByWhere(permission);
    }
}


